package com.fallsvc.re.javasesets.ebtree;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @auther falls_vc
 * description:
 * @date 2025/10/20  19:25
 */
public class Demo2 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("");

    }

    // 构建字符串
    // 输入：root = [1,2,3,null,4]
    // 输出："1(2()(4))(3)"
    public String tree2str(TreeNode root) {
        if(root == null) return null;
        StringBuilder s = new StringBuilder();
        createStr(root,s);

        return s.toString();

    }

    public void createStr(TreeNode root,StringBuilder s){
        if(root == null) return;

        s.append(root.val);
        if(root.left != null){
            s.append('(');
            createStr(root.left,s);
            s.append(')');
        }else{
            if(root.right != null) {
                s.append("()");
            }else{
                return;
            }
        }

        if(root.right != null) {
            s.append('(');
            createStr(root.right,s);
            s.append(')');
        }else {
            return;
        }
    }

    // 二叉树前序遍历 递归
    public List<Integer> preorderTraversal(TreeNode root) {

        List<Integer> list = new ArrayList<>();
        preorderTraversalChild(root,list);
        return list;
    }
    public void preorderTraversalChild(TreeNode root,List<Integer> list) {
        if(root == null) return;

        list.add(root.val);
        preorderTraversalChild(root.left,list);
        preorderTraversalChild(root.right,list);

    }
    // 非递归 Stack
    public List<Integer> preorderTraversal2(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        if(root==null){
            return list;
        }
        Stack<TreeNode> s=new Stack<>();
        TreeNode cur=root;
        while(cur!=null||!s.empty()){
            while(cur!=null){
                s.push(cur);
                list.add(cur.val);
                cur=cur.left;
            }
            TreeNode ret=s.pop();
            cur=ret.right;
        }
        return list;
    }
    // 非递归  中序遍历 Stack
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        if(root == null) return ret;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.empty()) {
            while(cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode t = stack.pop();
            ret.add(t.val);
            cur = t.right;
        }
        return ret;
    }

    // 非递归 后序遍历
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        if(root==null) return list;

        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        TreeNode prev=null;
        while(cur!=null||!stack.empty()){
            while(cur!=null){
                stack.push(cur);
                cur=cur.left;
            }
            TreeNode p=stack.peek();

            if(p.right==null||prev==p.right){
                list.add(p.val);
                prev=stack.pop();
            }else{
                cur=p.right;
            }
        }
        return list;
    }
}
